﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XF.FM.Table;
using XF.FM.TestEntities;

namespace XF.FM.Tests.Datatables
{
    [TestClass]
    public class TestDatatables
    {
        DbContext _db;
        TableRequest defaultRequest = new TableRequest() { Draw = 1, Start = 10, Length = 10 };
        protected DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
        {
            return _db.Set<TEntity>();
        }
        [TestInitialize]
        public void Init()
        {
            //new DbContextOptions<EfTestContext>();
            _db = new EfTestContext(new DbContextOptions<EfTestContext>());
        }
        [TestCleanup]
        public void Clean()
        {
            _db.Dispose();
        }
        [TestMethod]
        public void Test1()
        {
            int i = _db.Set<Film>().Count();
            Console.WriteLine(i);
            Assert.IsTrue(i > 0);
        }
        [TestMethod]
        public async Task Table_GetEfResult_1Async()
        {
            IQueryable<Film> query = _db.Set<Film>().AsQueryable();
            TableModel<Film> r = defaultRequest.GetEfResult(query);
            Assert.AreEqual(10, r.Data.Count());
            var data = _db.Set<Film>().First(m => m.FilmId == 3);
            
            //var entity= _db.Attach<Film>(data);
            //entity.State = EntityState.Modified;
            data.Title = "test3";
            int t =await _db.SaveChangesAsync();
            Assert.AreEqual(1, t);
        }
        [TestMethod]
        public void Table_GetEfResult_1_Sort()
        {
            IQueryable<Film> query = _db.Set<Film>().AsQueryable();
            TableModel<Film> r = defaultRequest.GetEfResult(query, false, m => m.LastUpdate);
            Assert.AreEqual(10, r.Data.Count());
        }
        [TestMethod]
        public void Table_GetEfResult_2()
        {
            IQueryable<Film> query = _db.Set<Film>().AsQueryable();
            TableModel<object> r = defaultRequest.GetEfResult<Film, object>(query, m => new { m.FilmId, m.Title, m.Rating, m.LastUpdate });
            Assert.AreEqual(10, r.Data.Count());
        }
        [TestMethod]
        public void Table_GetEfResult_3()
        {
            IQueryable<TestMany> query = from a in _db.Set<Film>()
                                       join b in _db.Set<Language>() on a.LanguageId equals b.LanguageId
                                       select new TestMany(){ F=a,L= b };
            //var test= query.OfType<TestMany>();
            //var t2= defaultRequest.GetEfResult(test);
            TableModel<TestMany> r = defaultRequest.GetEfResult(query.OrderByDescending(m=>m.F.FilmId));
            Assert.AreEqual(10, r.Data.Count());
        }        
        public class TestMany
        {
            public Film F { get; set; }
            public Language L { get; set; }
        }
    }
}
